package org.boulderfoodrescue.webapp.client.pickups.selectors;

import org.boulderfoodrescue.webapp.client.resources.Resources;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class CustomSelectorView extends Composite implements HasClickHandlers, HasText {

	private static DonorSelectorViewUiBinder uiBinder = GWT
			.create(DonorSelectorViewUiBinder.class);

	interface DonorSelectorViewUiBinder extends
	UiBinder<Widget, CustomSelectorView> {
	}

	public CustomSelectorView(String attribute) {
		this.attribute = attribute;
		initWidget(uiBinder.createAndBindUi(this));
		attributeLabel.setText(attribute);
		defaultAttributeText.setText("Select "+attribute);
		enabled = true;
	}

	@UiField
	Label attributeLabel;
	@UiField
	Label defaultAttributeText;

	private Boolean enabled;
	private String attribute;

	@UiHandler("defaultAttributeText")
	public void onDonorNameClick(ClickEvent event){
		if(enabled)
			this.fireEvent(event);
	}

	public void setEnabled(Boolean enabled){
		this.enabled = enabled;
		if(enabled)
			defaultAttributeText.removeStyleName(Resources.INSTANCE.customSelector().disabled());
		else
			defaultAttributeText.addStyleName(Resources.INSTANCE.customSelector().disabled());
	}

	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return addHandler(handler, ClickEvent.getType());
	}

	@Override
	public String getText() {
		return defaultAttributeText.getText();
	}

	@Override
	public void setText(String text) {
		defaultAttributeText.setText(attribute + " : " + text);
	}
}
